<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 图片区域 -->
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <!-- 表单区域 -->
            <el-form ref="loginRef" :model="loginForm" class="login_form" :rules="loginFormRules">
                <!-- 登录名 -->
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="iconfont icon-users"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input
                        v-model="loginForm.password"
                        type="password"
                        prefix-icon=" iconfont icon-3702mima"
                    ></el-input>
                </el-form-item>
                <!-- 按钮 -->
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                    <el-button>注册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      // 这是登录表单的数据对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        //   验证名字是否正确
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        //  验证密码是否正确
        password: [
          { required: true, message: '请输入活动名称', trigger: 'blur ' },
          { min: 3, max: 9, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    // 重置登录表单
    resetLoginForm () {
      console.log(this)
      this.$refs.loginRef.resetFields()
    },
    login () {
      console.log('点击')
      this.$refs.loginRef.validate(async valid => {
        console.log(valid)
        if (!valid) return
        const { data: res } = await this.axios.post('login', this.loginForm)
        console.log(res.data.token)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        // return this.$message.success('成功')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  },

  created () {}
}
</script>

<style lang='less' scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
</style>
